<?php

App::uses('AppController', 'Controller');

class FinanceController extends AppController {
    
    public $uses = array('Finance', 'FinanceCycle');
    
    public $paginate = array(
        'Finance' => array(
    		'limit' => ITEMS_PER_PAGE,
    		'order' => array(
    		    'Finance.id' => 'DESC',
    			'Finance.date'=>'DESC'
    		)
        ),
        'FinanceCycle' => array(
    		'limit' => ITEMS_PER_PAGE,
    		'order' => array(
    		    'FinanceCycle.date'=>'DESC'
    		)
        )
	);
    
    public function index() {
        $this->redirect("income");
    }
    
    public function income() {
        $data = $this->paginate('Finance', array('Finance.type' => 'income'));
        
        $this->set(compact('data'));
    }
    
    public function expense() {
        $data = $this->paginate('Finance', array('Finance.type' => 'expense'));
        
        $this->set(compact('data'));
    }
    
    public function create($type) {
        $data = $this->Finance->create(array('type'=>$type));
        
        $this->set(compact('type', 'data'));
        $this->render('form');
    }
    
    public function edit($id) {
        $data = $this->Finance->findById($id);
        
        $this->set(compact('data'));
        $this->render('form');
    }
    
    public function save() {
        if($this->request->is('post')) {
			
			$this->Finance->create($this->data);
			if($this->Finance->validates()) {
				
				if($this->Finance->saveAll($this->data)) {
					$this->setFlash(strtotitle(__('saved')) . ".", "ok");
					return $this->redirect(array('action'=>'edit', $this->Finance->id));
				}else{
					$this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
					$this->set("data", $this->data);
					$this->set("validationErrors", $this->Finance->validationErrors);
					return $this->render('form');
				}
			}else{
				$this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
				$this->set("data", $this->data);
				return $this->render('form');
			}
		}
		return $this->redirect(array('action'=>'index'));
    }
    
    public function delete($type, $id) {
        if($id>0) {
            if ($this->Finance->delete($id)) {
                $this->setFlash(strtotitle(__('deleted')) . ".", "ok");
            }else{
                $this->setFlash(strtotitle(__('delete failed')) . ".");
            }
        }
        $this->redirect('/finance/' . $type);
    }
    
    public function cycle() {
        $data = $this->paginate('FinanceCycle');
        
        $this->set(compact('data'));
    }
    
    public function create_cycle() {
        $data = $this->FinanceCycle->create();
        
        $this->set(compact('data'));
        $this->render('cycle_form');
    }
    public function edit_cycle($id) {
        $data = $this->FinanceCycle->findById($id);
        
        $this->set(compact('data'));
        $this->render('cycle_form');
    }
    public function save_cycle() {
        if($this->request->is('post')) {
			
			$this->FinanceCycle->create($this->data);
			if($this->FinanceCycle->validates()) {
				
				if($this->FinanceCycle->save($this->data)) {
					$this->setFlash(strtotitle(__('saved')) . ".", "ok");
					return $this->redirect(array('action'=>'edit_cycle', $this->FinanceCycle->id));
				}else{
					$this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
					$this->set("data", $this->data);
					$this->set("validationErrors", $this->FinanceCycle->validationErrors);
					return $this->render('cycle_form');
				}
			}else{
				$this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
				$this->set("data", $this->data);
				return $this->render('cycle_form');
			}
		}
		return $this->redirect(array('action'=>'cycle'));
    }
    
    public function delete_cycle($id) {
        if($id>0) {
            if ($this->FinanceCycle->delete($id)) {
                $this->setFlash(strtotitle(__('deleted')) . ".", "ok");
            }else{
                $this->setFlash(strtotitle(__('delete failed')) . ".");
            }
        }
        
        $this->redirect("cycle");
    }
    
}

?>